package com.yidu.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.dao.SnProDetailMapper;
import com.yidu.dao.SnProductionMapper;
import com.yidu.dao.SnQuDetailMapper;
import com.yidu.dao.SnQualityMapper;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnProduction;
import com.yidu.model.SnQuDetail;
import com.yidu.model.SnQuality;
import com.yidu.service.SnQualityService;
/**
 * 生产计划质检实现类
 * @author chensituo
 * @date 2018年11月29日 上午12:17:00
 */
@Service
public class SnQualityServiceImpl implements SnQualityService{
	/**
	 * 生产质检mapper
	 */
	@Resource
	SnQualityMapper qualityMapper;
	/**
	 * 生产质检mapper
	 */
	@Resource
	SnQuDetailMapper quDetailMapper;
	/**
	 *注入生产计划mapper
	 */
	@Resource
	SnProductionMapper productionMapper;
	
	/**
	 *注入生产详情mapper
	 */
	@Resource
	SnProDetailMapper proDetailMapper;
	/**
	 * 注入商品mapper
	 */
	@Resource
	SnCommodityMapper commodityMapper;
	
	@Override
	public int addAll(String proId) {
		SnProduction production=new SnProduction();
		production.setProId(proId);
		production.setProState(5);
		productionMapper.updateByPrimaryKeySelective(production);
		
		//查询生产详情集合
		List<SnProDetail> listDetail=proDetailMapper.findProId(proId);
		Integer count=0;
		for (SnProDetail snProDetail : listDetail) {
			count+=snProDetail.getDeNumber();
		}
		System.out.println("-----质检总数量："+count);
		
		//创建一个生产质检表对象
		SnQuality quality=new SnQuality();
		//将数据封装
		quality.setQuId(Tools.getDateOrderNo());
		quality.setProId(proId);
		quality.setQuCount(count);
		quality.setQuNotcount(0);
		quality.setQuAlready(0);
		quality.setQuProbability("0%");
		quality.setIsva(1);
		quality.setOptime(Tools.getCurDateTime());
		quality.setSort(Integer.valueOf(Tools.getTimeStamp()));
		quality.setOper("0");
		//调用生产计划质检增加的方法
		int rows1=qualityMapper.insertSelective(quality);
		//创建生产质检详情对象
		//遍历生产详情集合
		int rows2=0;
		for (SnProDetail snProDetail : listDetail) {
			
			SnCommodity snCommodity=commodityMapper.selectByPrimaryKey(snProDetail.getComId());
			
			//创建生产质检详情对象
			SnQuDetail quDetail=new SnQuDetail();
			//将数据封装
			quDetail.setQdId(Tools.getDateOrderNo());
			quDetail.setQuId(quality.getQuId());
			quDetail.setQdCount(snProDetail.getDeNumber());
			quDetail.setQdNotcount(0);
			quDetail.setQdAlready(0);
			quDetail.setQdProbability("0%");
			quDetail.setIsva(1);
			quDetail.setOptime(Tools.getCurDateTime());
			quDetail.setOper(snCommodity.getComName());
			quDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
			//调用增加的方法
			rows2=quDetailMapper.insertSelective(quDetail);
			if(rows2==0) {
				break;
			}
		}
		if(rows1!=0 && rows2!=0) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		//调用查询所有的方法
		return qualityMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用查询行数的方法
		return qualityMapper.findCount(maps);
	}
}
